<template>
	<view class="uni-tab-bar">
		<scroll-view id="tab-bar" class="uni-swiper-tab" scroll-x :scroll-left="scrollLeft" style="background: #fff;">
			<view v-for="(tab,index) in tabBars" :key="tab.id" :class="['swiper-tab-list',tabIndex==index ? 'active' : '']" :id="tab.id" :data-current="index" @tap="tapTab">{{tab.name}}</view>
		</scroll-view>
		<swiper :current="tabIndex" class="swiper-box" duration="300" @change="changeTab" :style="{'height': systemInfo +'px'}">
			<swiper-item v-for="(tab,index1) in newsitems" :key="index1">
				<scroll-view class="list" scroll-y @scrolltolower="loadMore(index1)">
					<!-- 普通商品 -->
					<view @click="changeisshow" style="background-color: #fff;height: 80upx;border-bottom: 1upx solid #ccc;display: flex;justify-content: space-between;padding: 0upx 50upx;">
						<view style="line-height: 80upx;">
							普通订单
						</view>
						<view style="line-height: 80upx;color: #ccc;">
							( {{tab.data.length}} ) >
						</view>
					</view>
					<block v-if="isshow == true" v-for="(newsitem,index2) in tab.data" :key="index2">
						<media-list :data="newsitem"  @cancel="cancel(newsitem.id)" @confirm='confirm(newsitem.id)' @log="log(newsitem.id)" @evaluate="evaluate(newsitem.id)" @del="del(newsitem.id)" @pay="pay(newsitem.order_number)" @click="goDetail(newsitem.id)"></media-list>
					</block>
					<view @click="changeisshow1" style="background-color: #fff;height: 80upx;border-bottom: 1upx solid #ccc;display: flex;justify-content: space-between;padding: 0upx 50upx;">
						<view style="line-height: 80upx;">
							拼团订单
						</view>
						<view style="line-height: 80upx;color: #ccc;">
							( {{tab.data_group.length}} ) >
						</view>
					</view>
					<block v-if="isshow1 == true" v-for="newsitem in tab.data_group">
						<media-list :data="newsitem"  @cancel="cancel(newsitem.id)" @confirm='confirm(newsitem.id)' @log="log(newsitem.id)" @evaluate="evaluate(newsitem.id)" @del="del(newsitem.id)" @pay="pay(newsitem.order_number)" @click="goDetail_group(newsitem.id)"></media-list>
					</block>
					<view @click="changeisshow2" style="background-color: #fff;height: 80upx;border-bottom: 1upx solid #ccc;display: flex;justify-content: space-between;padding: 0upx 50upx;">
						<view style="line-height: 80upx;">
							抢购订单
						</view>
						<view style="line-height: 80upx;color: #ccc;">
							( {{tab.data_limitTime.length}} ) >
						</view>
					</view>
					<block v-if="isshow2 == true" v-for="newsitem in tab.data_limitTime">
						<media-list :data="newsitem"  @cancel="cancel(newsitem.id)" @confirm='confirm(newsitem.id)' @log="log(newsitem.id)" @evaluate="evaluate(newsitem.id)" @del="del(newsitem.id)" @pay="pay(newsitem.order_number)" @click="goDetail_limitTime(newsitem.id)"></media-list>
					</block>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>
<script>
	import mediaList from '../../components/tab-nvue/mediaList.vue';
	// import uniLoadMore from '../../components/uni-load-more.vue';
	export default {
		components: {
			mediaList
			// uniLoadMore
		},
		data() {
			return {
				isshow: true,
				isshow1: true,
				isshow2: true,
				user_id:'',
				res:[],
				systemInfo:[],
				data0:[],
				data1:[],
				data2:[],
				data3:[],
				data4:[],
				data5:[],
				index0:1,
				index1:1,
				index2:1,
				index3:1,
				index4:1,
				index5:1,
				pagesize:1000,
				tabIndex: 0,
				scrollLeft: 0,
				isClickChange: false,
				loadingText: {
					contentdown: "上拉显示更多",
					contentrefresh: "正在加载...",
					contentnomore: "没有更多订单了"
				},
				newsitems: [{
					"loadingType":0,
					"data":[], // 普通订单
					"data_group":[], // 拼团订单
					"data_limitTime":[], // 限时订单
				},{
					"loadingType":0,
					"data":[],
					"data_group":[], // 拼团订单
					"data_limitTime":[],
				},{
					"loadingType":0,
					"data":[],
					"data_group":[], // 拼团订单
					"data_limitTime":[],
				},{
					"loadingType":0,
					"data":[],
					"data_group":[], // 拼团订单
					"data_limitTime":[],
				},{
					"loadingType":0,
					"data":[],
					"data_group":[], // 拼团订单
					"data_limitTime":[],
				},{
					"loadingType":0,
					"data":[],
					"data_group":[], // 拼团订单
					"data_limitTime":[],
				}],
				tabBars: [{
					name: '全部',
					id: 'guanzhu'
				}, {
					name: '待付款',
					id: 'tuijian'
				}, {
					name: '待发货',
					id: 'tiyu'
				}, {
					name: '已发货',
					id: 'redian'
				}, {
					name: '已完成',
					id: 'caijing'
				},{
					name: '已取消',
					id: 'nihao'
				}]
			}
		},
		onLoad: function(e) {
			uni.getStorage({
				key: 'user_id',
				success: (res) => {
					this.user_id = res.data
				}
			});
			uni.showLoading()
			this.tabIndex = -1
			setTimeout(() => {
				this.tabIndex = e.index
			}, 1000);
			uni.getSystemInfo({
				success: (res) => {
					this.systemInfo = res.windowHeight -50
				}
			})
			uni.getStorage({
				key: 'user_id',
				success: (res) => {
				 this.user_id = res.data
				 this.setAjax()
				},
			});
			
		},
		// onNavigationBarButtonTap:function(e){
		// 	uni.navigateTo({
		// 		url: 'recycle/recycle'
		// 	});
		//   },
		methods: {
			// 加载数据
			setAjax(){
				this.setData0()
				this.setData1()
				this.setData2()
				this.setData3()
				this.setData4()
				this.setData5()
			},
			setData0(){
				var o = this
				uni.request({
					url: 'https://cx.mingmeijt.com/Api/Order/index',
					method: 'POST',
					data: {'user_id': this.user_id},
					header: {'Content-Type': 'application/x-www-form-urlencoded'},
					success: res => {
					uni.hideLoading()
						if(this.index0 == 1){
							this.data0 = res.data.data.order_list.list
							for(var i = 0; i<o.data0.length; i++) {
								for(var j = 0; j < o.data0[i].goodsInfo.length; j++) {
									o.data0[i].goodsInfo[j].goods_thumb_image = 'https://cx.mingmeijt.com' + o.data0[i].goodsInfo[j].goods_thumb_image
								}
							}
							this.newsitems[0].data = this.data0
						}else{
							if(res.data.data.order_list.list.length == 0){
								this.newsitems[0].loadingType = 2;
								return;
							}else{
								this.data0 = this.data0.concat(res.data.data.order_list.list)
								this.newsitems[0].data = this.data0
							}
						}
					},
					fail: () => {},
					complete: () => {}
				});
				// 拼团订单
				uni.request({
					url: 'https://cx.mingmeijt.com/Api/AssembleOrder/index',
					method: 'POST',
					data:{
						user_id: this.user_id,
					},
					header:{ 'Content-Type':'application/x-www-form-urlencoded'},
					success: res => {
						let data_group = res.data.data.order_list.list
						for(var i = 0; i < data_group.length; i++) {
							data_group[i].goods_thumb = 'https://cx.mingmeijt.com' + data_group[i].goods_thumb
						}
						this.newsitems[0].data_group = data_group
					},
					fail: () => {},
					complete: () => {}
				});
				// 限时活动
				uni.request({
					url: 'https://cx.mingmeijt.com/Api/PanicBuyingOrder/index',
					method: 'POST',
					data:{
						user_id: this.user_id,
					},
					header:{ 'Content-Type':'application/x-www-form-urlencoded'},
					success: res => {
						console.log(res)
						let data_limitTime = res.data.data.order_list.list
						for(var i = 0; i < data_limitTime.length; i++) {
							data_limitTime[i].goods_thumb = 'https://cx.mingmeijt.com' + data_limitTime[i].goods_thumb
						}
						this.newsitems[0].data_limitTime = data_limitTime
					},
					fail: () => {},
					complete: () => {}
				});
			},
			setData1(){
				var o = this
				uni.request({
					url: 'https://cx.mingmeijt.com/Api/Order/index',
					method: 'POST',
					data: {'user_id': this.user_id},
					header: {'Content-Type': 'application/x-www-form-urlencoded'},
					success: res => {
					uni.hideLoading()
						if(this.index1 == 1){
							let fil = res.data.data.order_list.list
							fil = fil.filter(function(item,index,array){
									//元素值，元素的索引，原数组
									return (item.status == "1" || item.status == 1);
							})
							this.data1 = fil
							
							for(var i = 0; i < o.data1.length; i++) {
								for(var j = 0; j < o.data1[i].goodsInfo.length; j++) {
									o.data1[i].goodsInfo[j].goods_thumb_image = 'https://cx.mingmeijt.com' + o.data1[i].goodsInfo[j].goods_thumb_image
								}
							}
							this.newsitems[1].data = this.data1
						} else {
							if (res.data.data.order_list.list.length == 0) {
								this.newsitems[1].loadingType = 2;
								return;
							} else {
								this.data1 = this.data1.concat(res.data.data.order_list.list)
								this.newsitems[1].data = this.data1
							}
						}
					},
					fail: () => {},
					complete: () => {
					}
				});
				// 拼团订单
				uni.request({
					url: 'https://cx.mingmeijt.com/Api/AssembleOrder/index',
					method: 'POST',
					data:{
						user_id: this.user_id,
					},
					header:{ 'Content-Type':'application/x-www-form-urlencoded'},
					success: res => {
						let data_group = res.data.data.order_list.list
						data_group = data_group.filter(function(item,index,array){
								return (item.status == "1" || item.status == 1);
						})
						for(var i = 0; i < data_group.length; i++) {
							data_group[i].goods_thumb = 'https://cx.mingmeijt.com' + data_group[i].goods_thumb
						}
						this.newsitems[1].data_group = data_group
					},
					fail: () => {},
					complete: () => {}
				});
			},
			setData2(){
				var o = this
				uni.request({
					url: 'https://cx.mingmeijt.com/Api/Order/index',
					method: 'POST',
					data: {'user_id': this.user_id},
					header: {'Content-Type': 'application/x-www-form-urlencoded'},
					success: res => {
					uni.hideLoading()
						if(this.index2 == 1){
							let fil = res.data.data.order_list.list
							fil = fil.filter(function(item,index,array){
									//元素值，元素的索引，原数组
									return (item.status == "2" || item.status == 2);
							})
							this.data2 = fil
							
							for(var i = 0; i<o.data2.length; i++) {
								for(var j = 0; j < o.data2[i].goodsInfo.length; j++) {
									o.data2[i].goodsInfo[j].goods_thumb_image = 'https://cx.mingmeijt.com' + o.data2[i].goodsInfo[j].goods_thumb_image
								}
							}
							this.newsitems[2].data = this.data2
						}else{
							if(res.data.data.order_list.list.length == 0){
								this.newsitems[2].loadingType = 2;
								return;
							}else{
								this.data2 = this.data2.concat(res.data.data.order_list.list)
								this.newsitems[2].data = this.data2
							}
						}
					},
					fail: () => {},
					complete: () => {
					}
				});
				// 拼团订单
				uni.request({
					url: 'https://cx.mingmeijt.com/Api/AssembleOrder/index',
					method: 'POST',
					data:{
						user_id: this.user_id,
					},
					header:{ 'Content-Type':'application/x-www-form-urlencoded'},
					success: res => {
						let data_group = res.data.data.order_list.list
						data_group = data_group.filter(function(item,index,array){
								return (item.status == "2" || item.status == 2);
						})
						for(var i = 0; i < data_group.length; i++) {
							data_group[i].goods_thumb = 'https://cx.mingmeijt.com' + data_group[i].goods_thumb
						}
						this.newsitems[2].data_group = data_group
					},
					fail: () => {},
					complete: () => {}
				});
			},
			setData3(){
				var o = this
				uni.request({
					url: 'https://cx.mingmeijt.com/Api/Order/index',
					method: 'POST',
					data: {'user_id': this.user_id},
					header: {'Content-Type': 'application/x-www-form-urlencoded'},
					success: res => {
					uni.hideLoading()
						if(this.index3 == 1){
							let fil = res.data.data.order_list.list
							fil = fil.filter(function(item,index,array){
									//元素值，元素的索引，原数组
									return (item.status == "3" || item.status == 3);
							})
							this.data3 = fil
							
							for(var i = 0; i<o.data3.length; i++) {
								for(var j = 0; j < o.data3[i].goodsInfo.length; j++) {
									o.data3[i].goodsInfo[j].goods_thumb_image = 'https://cx.mingmeijt.com' + o.data3[i].goodsInfo[j].goods_thumb_image
								}
							}
							this.newsitems[3].data = this.data3
						}else{
							if(res.data.data.order_list.list.length == 0){
								this.newsitems[3].loadingType = 2;
								return;
							}else{
								this.data3 = this.data3.concat(res.data.data.order_list.list)
								this.newsitems[3].data = this.data3
							}
						}
					},
					fail: () => {},
					complete: () => {
					}
				});
				// 拼团订单
				uni.request({
					url: 'https://cx.mingmeijt.com/Api/AssembleOrder/index',
					method: 'POST',
					data:{
						user_id: this.user_id,
					},
					header:{ 'Content-Type':'application/x-www-form-urlencoded'},
					success: res => {
						let data_group = res.data.data.order_list.list
						data_group = data_group.filter(function(item,index,array){
								return (item.status == "3" || item.status == 3);
						})
						for(var i = 0; i < data_group.length; i++) {
							data_group[i].goods_thumb = 'https://cx.mingmeijt.com' + data_group[i].goods_thumb
						}
						this.newsitems[3].data_group = data_group
					},
					fail: () => {},
					complete: () => {}
				});
			},
			setData4(){
				var o = this
				uni.request({
					url: 'https://cx.mingmeijt.com/Api/Order/index',
					method: 'POST',
					data: {'user_id': this.user_id},
					header: {'Content-Type': 'application/x-www-form-urlencoded'},
					success: res => {
					uni.hideLoading()
						if(this.index4 == 1){
							let fil = res.data.data.order_list.list
							fil = fil.filter(function(item,index,array){
									//元素值，元素的索引，原数组
									return (item.status == "4" || item.status == 4);
							})
							this.data4 = fil
							for(var i = 0; i<o.data4.length; i++) {
								for(var j = 0; j < o.data4[i].goodsInfo.length; j++) {
									o.data4[i].goodsInfo[j].goods_thumb_image = 'https://cx.mingmeijt.com' + o.data4[i].goodsInfo[j].goods_thumb_image
								}
							}
							this.newsitems[4].data = this.data4
						}else{
							if(res.data.data.order_list.list.length == 0){
								this.newsitems[4].loadingType = 2;
								return;
							}else{
								this.data4 = this.data4.concat(res.data.data.order_list.list)
								this.newsitems[4].data = this.data4
							}
						}
					},
					fail: () => {},
					complete: () => {
					}
				});
				// 拼团订单
				uni.request({
					url: 'https://cx.mingmeijt.com/Api/AssembleOrder/index',
					method: 'POST',
					data:{
						user_id: this.user_id,
					},
					header:{ 'Content-Type':'application/x-www-form-urlencoded'},
					success: res => {
						let data_group = res.data.data.order_list.list
						data_group = data_group.filter(function(item,index,array){
								return (item.status == "4" || item.status == 4);
						})
						for(var i = 0; i < data_group.length; i++) {
							data_group[i].goods_thumb = 'https://cx.mingmeijt.com' + data_group[i].goods_thumb
						}
						this.newsitems[4].data_group = data_group
					},
					fail: () => {},
					complete: () => {}
				});
			},
			setData5(){
				var o = this
				uni.request({
					url: 'https://cx.mingmeijt.com/Api/Order/index',
					method: 'POST',
					data: {'user_id': this.user_id},
					header: {'Content-Type': 'application/x-www-form-urlencoded'},
					success: res => {
					uni.hideLoading()
						if(this.index5 == 1){
							let fil = res.data.data.order_list.list
							fil = fil.filter(function(item,index,array){
									return (item.status == "5" || item.status == 5);
							})
							this.data5 = fil
							for(var i = 0; i<o.data5.length; i++) {
								for(var j = 0; j < o.data5[i].goodsInfo.length; j++) {
									o.data5[i].goodsInfo[j].goods_thumb_image = 'https://cx.mingmeijt.com' + o.data5[i].goodsInfo[j].goods_thumb_image
								}
							}
							this.newsitems[5].data = this.data5
						}else{
							if(res.data.data.order_list.list.length == 0){
								this.newsitems[5].loadingType = 2;
								return;
							}else{
								this.data5 = this.data5.concat(res.data.data.order_list.list)
								this.newsitems[5].data = this.data5
							}
						}
					},
					fail: () => {},
					complete: () => {
					}
				});
				// 拼团订单
				uni.request({
					url: 'https://cx.mingmeijt.com/Api/AssembleOrder/index',
					method: 'POST',
					data:{
						user_id: this.user_id,
					},
					header:{ 'Content-Type':'application/x-www-form-urlencoded'},
					success: res => {
						let data_group = res.data.data.order_list.list
						data_group = data_group.filter(function(item,index,array){
								return (item.status == "5" || item.status == 5);
						})
						for(var i = 0; i < data_group.length; i++) {
							data_group[i].goods_thumb = 'https://cx.mingmeijt.com' + data_group[i].goods_thumb
						}
						this.newsitems[5].data_group = data_group
					},
					fail: () => {},
					complete: () => {}
				});
			},
			// 加载更多
			loadMore(e) {
				// this.newsitems[e].loadingType = 1;
				// this.addData(e);
			},
			changeisshow:function(){
				this.isshow = !this.isshow
			},
			changeisshow1:function(){
				this.isshow1 = !this.isshow1
			},
			changeisshow2:function(){
				this.isshow2 = !this.isshow2
			},
			addData(e) {
				if(e == 0){
					this.index0 ++
					this.setData0()
				}else if(e == 1){
					this.index1 ++
					this.setData1()
				}else if(e == 2){
					this.index2 ++
					this.setData2()
				}else if(e == 3){
					this.index3 ++
					this.setData3()
				}else if(e == 4){
					this.index4 ++
					this.setData4()
				}
			},
			// 操作功能
			cancel(id) {
				//取消订单
				var itemList1 = ['我不想买了', '信息填写错误，重新拍', '其他原因']
				uni.showActionSheet({
					itemList: itemList1,
					success: (e) => {
						uni.request({
							url: 'http://suhua.suzhanxue.com/app/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=order.op.cancel&app=1',
							method: 'POST',
							data: {
								'openid':this.openid,
								'id':id,
								'remark':itemList1[e.tapIndex],
							},
							header: {'Content-Type': 'application/x-www-form-urlencoded'},
							success: res => {
								uni.showToast({
									title:res.data.result.message,
									icon:"none"
								})
								uni.showLoading()
								this.setAjax()
								this.tabIndex = -1
								setTimeout(() => {
									uni.hideLoading()
									this.tabIndex = 0
								}, 1000);
							},
							fail: () => {},
							complete: () => {}
						});
					}
				})
			},
			del(id) {
				// 删除订单
				uni.showModal({
					content: '确认要删除该订单吗？',
					success: (res) => {
						if (res.confirm) {
							uni.request({
								url: 'http://suhua.suzhanxue.com/app/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=order.op.delete&app=1&orderid='+id+'&userdeleted=1',
								method: 'POST',
								data: {'openid':this.openid},
								header: {'Content-Type': 'application/x-www-form-urlencoded'},
								success: res => {
									uni.showLoading()
									this.setAjax()
									this.tabIndex = -1
									setTimeout(() => {
										uni.hideLoading()
										this.tabIndex = 0
									}, 1000);
								},
								fail: () => {},
								complete: () => {}
							});
						}
					}
				})
			},
			confirm(id){
				// 确认收货
				uni.showModal({
					content: '确认已收到货了吗？',
					success: (res) => {
						if (res.confirm) {
							uni.request({
								url: 'http://suhua.suzhanxue.com/app/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=order.op.finish&app=1&orderid='+id,
								method: 'POST',
								data: {'openid':this.openid},
								header: {'Content-Type': 'application/x-www-form-urlencoded'},
								success: res => {
									uni.showLoading()
									this.setAjax()
									setTimeout(() => {
										uni.hideLoading()
										this.tabIndex = 4
									}, 1000);
								},
								fail: () => {},
								complete: () => {}
							});
						}
					}
				})
			},
			// 跳转操作
			goDetail(id) {
				uni.navigateTo({
					url: 'detail/detail?id=' + id
				})
			},
			goDetail_group (id) {
				uni.navigateTo({
					url: 'detail/detail?group_id=' + id
				})
			},
			goDetail_limitTime (id) {
				uni.navigateTo({
					url: 'detail/detail?limitTime_id=' + id
				})
			},
			evaluate(id){
				uni.navigateTo({
					url: 'comment/comment?id=' + id
				});
			},
			log(id){
				uni.navigateTo({
					url: 'express/express?id='+id
				});
			},
			pay(order_number){
				let o = this
				uni.request({
					url: 'https://cx.mingmeijt.com/Api/Pay/payOrder',
					method: 'POST',
					header:{'Content-Type':'application/x-www-form-urlencoded'},
					data:{
						user_id: this.user_id,
						order_number: order_number,
					},
					success: res => {
						uni.showToast({
							title: '支付成功'
						})
						setTimeout(function() {
							o.setData1()
						}, 1500);
					},
					fail: () => {},
					complete: () => {}
				});
				
				// uni.navigateTo({
				// 	url:'pay/pay?id='+id
				// });
			},
			// tab切换功能
			async changeTab(e) {
				let index = e.target.current;
				if (this.isClickChange) {
					this.tabIndex = index;
					this.isClickChange = false;
					return;
				}
				let tabBar = await this.getElSize("tab-bar"),
					tabBarScrollLeft = tabBar.scrollLeft;
				let width = 0;

				for (let i = 0; i < index; i++) {
					let result = await this.getElSize(this.tabBars[i].id);
					width += result.width;
				}
				let winWidth = uni.getSystemInfoSync().windowWidth,
					nowElement = await this.getElSize(this.tabBars[index].id),
					nowWidth = nowElement.width;
				if (width + nowWidth - tabBarScrollLeft > winWidth) {
					this.scrollLeft = width + nowWidth - winWidth;
				}
				if (width < tabBarScrollLeft) {
					this.scrollLeft = width;
				}
				this.isClickChange = false;
				this.tabIndex = index; //一旦访问data就会出问题
			},
			getElSize(id) { //得到元素的size
				return new Promise((res, rej) => {
					uni.createSelectorQuery().select("#" + id).fields({
						size: true,
						scrollOffset: true
					}, (data) => {
						res(data);
					}).exec();
				})
			},
			async tapTab(e) { //点击tab-bar
				if (this.tabIndex == e.target.dataset.current) {
					return false;
				} else {
					this.isClickChange = true;
					this.tabIndex = e.target.dataset.current
				}
			}
		}
	}
</script>

<style>uni-page-body{height:100%;}</style>